<?php

namespace common\models\farmerhouse;

use common\models\business\BusinessAdress;
use common\models\business\BusinessFarmers;
use common\models\business\BusinessPerson;
use common\models\shop\ShopHouseOrder;
use Yii;
use yii\db\Query;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "farmerhouse_house".
 *
 * @property string $id
 * @property integer $personid
 * @property string $name
 * @property integer $farmerid
 * @property string $envdesc
 * @property string $accommodation
 * @property integer $roomnum
 * @property integer $canfeed
 * @property integer $caninnum
 * @property string $price
 * @property string $envphoto
 * @property string $accommodationphoto
 * @property integer $createtime
 * @property string $detail
 * @property string $feedlive
 * @property integer $isrecommand
 * @property integer $status
 * @property integer $city
 * @property integer $county
 * @property integer $town
 * @property string $address
 */
class FarmerhouseHouse extends \yii\db\ActiveRecord
{



    const RECOMMAND = 1;
    const NORECOMMAND = 0;
    public static $isrecommand = [
        self::NORECOMMAND => '不推荐',
        self::RECOMMAND => '推荐',
    ];


    //状状态，0，未审核，1，审核通过，-1，审核不通过
    const STATUS_NOLOOK = 0;
    const STATUS_AGREE = 1;
    const STATUS_DISAGREE = -1;
    public static $statues = [
        self::STATUS_NOLOOK => '未审核',
        self::STATUS_AGREE => '审核通过',
        self::STATUS_DISAGREE => '审核不通过',
    ];

    const CANFEED = 1;
    const DISCANFEED = 0;
    public static $canfeed = [
        self::CANFEED => '提供',
        self::DISCANFEED => '不提供',
    ];



    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '农家乐',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => ['envphoto','accommodationphoto'],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>['createtime'],      //不需要记录的字段
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%farmerhouse_house}}';
    }



    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['personid', 'name', 'farmerid', 'envdesc', 'accommodation', 'roomnum', 'canfeed', 'caninnum', 'price', 'createtime', 'detail', 'feedlive', 'status', 'city', 'county', 'town', 'address'], 'required'],
            [['personid', 'farmerid', 'roomnum', 'canfeed', 'caninnum', 'createtime', 'isrecommand', 'status', 'city', 'county', 'town'], 'integer'],
            [['price'], 'number'],
            [['detail', 'feedlive'], 'string'],
            [['name', 'address'], 'string', 'max' => 255],
            [['envphoto','accommodationphoto'], 'image', 'extensions' => 'jpg,jpeg,png','on'=>['uodate','insert']],
        ];
    }



    public function scenarios()
    {
        $scenarios = parent::scenarios();//本行必填，不写的话就会报如上错误
        $scenarios['create'] = ['envphoto', 'accommodationphoto'];
        $scenarios['update'] = ['envphoto', 'accommodationphoto'];

        return $scenarios;
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'personid' => '推荐信息提交人',
            'name' => '农家乐名称',
            'farmerid' => '对应的农户',
            'envdesc' => '环境描述',
            'accommodation' => '住宿情况',
            'roomnum' => '房间数',
            'canfeed' => '是否提供餐',
            'caninnum' => '可住人数',
            'price' => '价格',
            'envphoto' => '环境照片',
            'accommodationphoto' => '住宿照片',
            'createtime' => '提交时间',
            'detail' => '详细介绍',
            'feedlive' => '食宿情况',
            'isrecommand' => '是否推荐',
            'status' => '状态',
            'city' => '城市',
            'county' => '县',
            'town' => '乡镇',
            'address' => '详细地址',
        ];
    }


    public function beforeSave($insert)
    {
        $this->createtime = time();
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取推荐人
     */
    public function getPerson()
    {
        return $this->hasOne(BusinessPerson::className(),['id'=>'personid']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取农户
     */
    public function getFarmer()
    {
        return $this->hasOne(BusinessFarmers::className(),['id'=>'farmerid']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取城市
     */
    public function getAcity()
    {
        return $this->hasOne(BusinessAdress::className(),['id'=>'city']);
    }

    public function getAcounty()
    {
        return $this->hasOne(BusinessAdress::className(),['id'=>'county']);
    }

    public function getAtown()
    {
        return $this->hasOne(BusinessAdress::className(),['id'=>'town']);
    }



    public static function getLeftnum($id,$startdate,$enddate){
        $orders = (new Query())->select('sum(roomnum) as roomnum')->from(ShopHouseOrder::tableName())
            ->where('startdate>=:startdate and startdate<=:enddate and status>=:status and houseid=:hid',[':startdate'=>$startdate,':enddate'=>$enddate,':status'=>ShopHouseOrder::STATUS_PAY,':hid'=>$id])
            ->groupBy('startdate')
            ->all();
        //取出农家乐总共可以住多少人
        $rooms = self::find()->select('roomnum')->where('id=:id',[':id'=>$id])->asArray()->scalar();
        $num = $rooms;
        if(!empty($orders)){
            $roomnum = ArrayHelper::getColumn($orders,'roomnum');
            //剩余房间数
            asort($roomnum);
            $minroom = array_pop($roomnum);
            //剩余房间数
            $num = $rooms - $minroom;
        }
        return $num;
    }




}
